<template>
  <div>
    <!-- 弹出层打开 初始化数据 -->
    <van-popup :value="isShow" position="right" @open="closed">
      <van-nav-bar :title="total + '条回复'" left-arrow @click-left="leftClick">
      </van-nav-bar>
      <div class="reply-wrapper list">
        <!-- 列表 -->
        <div class="item van-hairline--bottom">
          <van-image round width="10vw" height="10vw" :src="comId.aut_photo" />
          <div class="info">
            <p>
              <span class="name">{{ comId.aut_name }}</span>
              <span class="zanTop">关注</span>
              <span class="zanBottom"
                >{{ comId.like_count }}<geek-icon name="like2"
              /></span>
            </p>
            <p class="cont">{{ comId.content }}</p>
            <p>
              <span class="time" style="margin-left:0">{{
                comId.pubdate | filterTimeGobal
              }}</span>
            </p>
          </div>
        </div>
        <h3 class="comTitle">回复全部</h3>
        <van-list
          v-if="bol"
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getDate"
        >
          <div
            class="item van-hairline--bottom"
            v-for="i in commList"
            :key="i.com_id"
          >
            <van-image round width="10vw" height="10vw" :src="i.aut_photo" />
            <div class="info">
              <p>
                <span class="name">{{ i.aut_name }}</span>
                <span class="zan"
                  >{{ i.like_count }}<geek-icon name="like2"
                /></span>
              </p>
              <p class="cont">{{ i.content }}</p>
              <p>
                <span class="time" style="margin-left:0">{{
                  i.pubdate | filterTimeGobal
                }}</span>
              </p>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 底 -->
      <div class="footer van-hairline--top" style="position:static">
        <div class="input big" @click="showClick">
          <i class="van-icon van-icon-edit"></i>
        </div>
        <div class="btn">
          <geek-icon name="collect"></geek-icon>
          <p>收藏</p>
        </div>
        <div class="btn">
          <geek-icon name="share"></geek-icon>
          <p>分享</p>
        </div>
      </div>
    </van-popup>
    <van-popup
      position="bottom"
      v-model="ShowPopup"
      @close="closed2"
      v-if="bol"
    >
      <van-nav-bar
        :title="comId !== '' ? '回复评论' : '发表评论'"
        left-arrow
        @click-left="leftClick2"
      >
        <template #right>
          <div style="color:#fc6627" @click="sendComm">发表</div>
        </template>
      </van-nav-bar>
      <van-form ref="form">
        <van-field
          v-model="text"
          :rules="rules"
          rows="3"
          autosize
          type="textarea"
          maxlength="100"
          :placeholder="comId.com_id ? `@${comId.aut_name}` : '请输入评论'"
          show-word-limit
        >
        </van-field>
      </van-form>
    </van-popup>
  </div>
</template>
<script>
import { comments, commentsPost } from '@/api/article.js'
export default {
  name: 'ReComment',
  props: {
    isShow: {
      typeof: Boolean,
      default: false
    },
    comId: {
      type: [String, Object],
      default: ''
    },
    info: {
      type: [String, Object],
      default: () => ({})
    },
    commentList: {
      type: Array,
      default: () => []
    }
  },

  model: {
    prop: 'isShow',
    event: 'closePopup'
  },
  data () {
    return {
      total: 0,
      text: '',
      ShowPopup: false,
      bol: true,
      loading: false,
      finished: false,
      commList: [],
      params: {
        type: 'c', // 是,a或c评论类型，a-对文章(article)的评论，c-对评论(comment)的回复
        source: '', // 是,源id，文章id或评论id
        offset: null, // 否,获取评论数据的偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数据
        limit: 10 // 否,获取的评论数据个数，不传表示采用后端服务设定的默认每页数据量
      },
      rules: [{ required: true, message: '请输入内容', trigger: 'onChange' }]
    }
  },
  created () {
    // this.getDate()
    // console.log(this.$route.query.id)
  },
  methods: {
    leftClick () {
      this.$emit('closePopup', false)
    },
    async getDate () {
      const res = await comments({
        ...this.params,
        source: this.comId.com_id
        // source: '6b1b01e5-63ca-468a-b26a-35b4f6bcdf8c'
      })
      this.loading = false
      this.commList.push(...res.data.results)
      if (res.data.last_id === res.data.end_id) {
        this.finished = true
      } else {
        this.params.offset = res.data.last_id
      }
      this.total = res.data.total_count
      console.log('评论回复数据', res)
    },
    closed () {
      // 关闭弹出层，初始化数据
      this.loading = false
      this.finished = false
      this.commList = []
      //  初始化组件
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    leftClick2 () {
      this.ShowPopup = false
    },
    showClick () {
      this.ShowPopup = true
    },
    async sendComm () {
      // 验证表单，非空判断
      await this.$refs.form.validate()
      if (this.comId !== '') {
        const res = await commentsPost({
          target: this.comId.com_id,
          content: this.text,
          art_id: this.$route.query.id
        })
        // 将新产生的评论添加到评论列表中
        this.commList.unshift({ ...res.data.new_obj })
        //  点击发表，清空文本
        this.text = ''
        this.ShowPopup = false
        this.total++
        console.log('回复评论返回的数据', res)
      } else {
        //  发表评论
        const res = await commentsPost({
          target: this.$route.query.id,
          content: this.text
        })
        this.commentList.unshift(res.data.new_obj)
        this.ShowPopup = false
        this.info.comm_count++
        console.log('文章评论返回数据', res)
      }
    },
    closed2 () {
      //  点击发表，清空文本
      this.text = ''
      // 初始化组件
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    }
  }
}
</script>

<style lang="less" scoped>
.van-popup {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.reply-wrapper {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
.article-comment {
  .title {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    background: #fff;
    span {
      font-size: 16px;
      &:last-child {
        color: #ccc;
        font-size: 14px;
      }
    }
  }
  .list {
    padding: 0 16px 35px;
    .item {
      display: flex;
      padding: 10px 0;
      .info {
        padding-left: 10px;
        flex: 1;
        p {
          margin: 0;
          .name {
            font-size: 16px;
          }
          .zan,
          .follow {
            font-size: 14px;
            float: right;
            color: #999;
            .geek-icon {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }
          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }
          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;
            .van-icon {
              position: relative;
              top: 1px;
            }
          }
          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }
}

.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 50px;
  background: #fff;
  display: flex;
  width: 100%;
  align-items: center;
  .input {
    margin-left: 10px;
    width: 200px;
    height: 34px;
    background: @geek-gray-color;
    border-radius: 17px;
    line-height: 36px;
    padding-left: 10px;
    .van-icon {
      color: #999;
    }
    &.big {
      width: 260px;
    }
  }
  .btn {
    flex: 1;
    text-align: center;
    position: relative;
    p {
      margin: 0;
      font-size: 10px;
    }
    .geek-icon {
      font-size: 18px;
    }
    i {
      height: 16px;
      min-width: 16px;
      padding: 0 3px;
      background: @geek-color;
      color: #fff;
      font-size: 10px;
      position: absolute;
      right: 0;
      top: -4px;
      line-height: 16px;
      border-radius: 8px;
      font-style: normal;
    }
  }
}
::v-deep .van-nav-bar__text {
  color: @geek-color;
}
::v-deep .van-field__control {
  background: @geek-gray-color;
  padding: 5px 10px;
  margin-bottom: 5px;
  border-radius: 4px;
}
.comTitle {
  font-size: 16px;
  font-weight: 400;
  margin-top: 20px;
  margin-bottom: 20px;
}
.zanTop {
  font-size: 14px;
  float: top;
  color: rgb(255, 27, 27);
  margin-top: 10px;
  margin-left: 249px;
}
.zanBottom {
  font-size: 14px;
  float: right;
  color: #999;
  margin-top: 56px;
  .geek-icon {
    font-size: 12px;
    position: relative;
    top: -1px;
  }
}
</style>
